package hotciv.view;

import java.awt.event.MouseEvent;

import minidraw.standard.NullTool;
import hotciv.framework.*;

public class TileFocusTool extends NullTool {
	
	// We *need* a Game instance to work with! But we have to get it from the constructor..
	private final Game game;
	
	// Field to remember the "down" position.
	private Position posDown;
	
	/**
	 * Create a Tile Focus Tool
	 * @param game the game whose tiles you want to access.
	 */
	public TileFocusTool (Game game) {
		this.game = game;
	}
	
	@Override
	public void mouseDown(MouseEvent event, int x, int y) {
		// Remeber the "down" position
		posDown = GfxConstants.getPositionFromXY(x, y);
	}
	
	@Override
	public void mouseUp(MouseEvent event, int x, int y) {
		// Check if mouse is clicked within a tile.
		// If the mouse is released from another tile,
		// than from which it was pressed down,
		// then it was not clicked but dragged! No action.
		Position posUp = GfxConstants.getPositionFromXY(x, y);
		if (posDown.inWorld() && posDown.equals(posUp)) {
			// The mouse was clicked on a tile within the world.
			// Invoke the set tile focus
			game.setTileFocus(posDown);
		}
		// Forget the "down" position
		posDown = null;
	}

}
